package com.example.smartcity.ui.smartCity

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.example.smartcity.R
import com.example.smartcity.SmartCityApplication
import com.example.smartcity.databinding.FragmentSmartcityBinding
import com.example.smartcity.ui.AcNews.NewSFlashShowActivity
import com.example.smartcity.ui.smartCity.Adapter.SmartCityRecyclerViewAdapter
import com.example.smartcity.ui.smartCity.model.SmartCityEntity
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator

class SmartCityFragment : Fragment() {
    /**
     * 懒加载
     */
    private val viewModel by lazy {
        ViewModelProvider(this)[SmartCityViewModel::class.java]
    }

    // RecyclerView适配器
    private val ReList = ArrayList<SmartCityEntity>()

    private var _binding: FragmentSmartcityBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentSmartcityBinding.inflate(inflater, container, false)

        // 设置轮播图
        setBanner(this)

        // 设置模块
        setRecyclerView()

        return binding.root
    }

    /**
     * 设置模块
     */
    private fun setRecyclerView() {
        ReList.add(SmartCityEntity(SmartCityApplication.url + "/prod-api/profile/upload/image/2021/05/06/b9d9f081-8a76-41dc-8199-23bcb3a64fcc.png", "党建动态", "党建动态新闻"))
        ReList.add(SmartCityEntity(SmartCityApplication.url + "/prod-api/profile/upload/image/2021/05/06/e614cb7f-63b0-4cda-bf47-db286ea1b074.png", "党员学习", "党员学习二十大思想"))
        ReList.add(SmartCityEntity(SmartCityApplication.url + "/prod-api/profile/upload/image/2021/05/06/242e06f7-9fb0-4e16-b197-206f999c98f2.png", "建言献策", "建言献策描述"))
        ReList.add(SmartCityEntity(SmartCityApplication.url + "/prod-api/profile/upload/image/2021/05/06/e614cb7f-63b0-4cda-bf47-db286ea1b074.png", "随手拍", "随手拍描述"))

        // 设置RecyclerView适配器
        val layout = GridLayoutManager(context, 2)
        binding.SmRecyclerView.layoutManager = layout
        val adapter = SmartCityRecyclerViewAdapter(ReList)
        binding.SmRecyclerView.adapter = adapter

        binding.isRecyclerView.visibility = View.VISIBLE
    }


    /**
     * TODO 设置Banner
     */
    private fun setBanner(a: LifecycleOwner) {
        // 存储照片集合
        val listImageView = ArrayList<String>()

        // 发起网络请求
        viewModel.slideshow(mutableMapOf<String, Int>().apply {
            this["pageNum"] = 1
            this["pageSize"] = 8
            this["type"] = 2
        })

        // 获取数据
        viewModel.placeLiveData.observe(this, Observer { resources ->
            val body = resources.getOrNull()
            if(body != null){
                // 清空数据
                viewModel.placeList.clear()
                // 添加数据
                viewModel.placeList.addAll(body)
            }
            // 取出数据
            for(i in viewModel.placeList) {
                if (listImageView.size < viewModel.placeList.size) {
                    listImageView.add(SmartCityApplication.url + i.advImg)
                }
            }

            // 轮播图适配器
            binding.SmBanner.setBannerRound(25F)
            // 设置指示器
            binding.SmBanner.setAdapter(object : BannerImageAdapter<String>(listImageView){
                override fun onBindView(
                    holder: BannerImageHolder?,
                    data: String?,
                    position: Int,
                    size: Int
                ) {
                    data?.let {
                        holder?.let {
                            context?.let {
                                    it1 -> Glide.with(it1).load(data).into(holder.imageView)
                            }
                        }
                    }
                }
            }).addBannerLifecycleObserver(a).indicator = CircleIndicator(context)

            binding.SmBanner.setOnBannerListener { data, position ->
                val intent = Intent(context, NewSFlashShowActivity::class.java)
                intent.putExtra("model", position.toString())
                startActivity(intent)
            }
        })



        // 将布局显示出来
        binding.isBanner.visibility = View.VISIBLE
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}